<template>
  <view>
		<u-navbar back-icon-size="30" back-icon-color="#2D2E32" title="授权微信" title-color="#2D2E32"></u-navbar>
		<view class="logo">
			<img src="../static/login/login.png" alt="" style="width: 530rpx;height: 530rpx;"/>
		</view>
		<view class="flex flex-column align-center" style="padding-top: 100rpx;">
			<dy-button width="576rpx" size="default" color="#fff" :backgroundColor="'var(--mainColor)'" @click="login">授权微信</dy-button>
			<view class="u-m-t-30">
				<u-checkbox shape="circle" v-model="checked">
					<text>登录即代表您已经阅读同意</text>
					<text class="text-primary" @click.stop="toArgu('register_credit')">《用户服务协议》</text>
				</u-checkbox>
				<view class="text-center">
					<text class="text-primary" @click.stop="toArgu('privacy_agreement')">《隐私政策》</text>
				</view>
			</view>
		</view>
  </view>
</template>

<script>
  import { BaseCenterApi } from "@/api/serves.js"
  import { mapMutations, mapActions } from 'vuex'
  export default {
    data() {
			return {
				checked: false,
        loginCode: ""
			}
		},
    onReady() {
      this.getCode()
		},
    methods: {
      ...mapMutations({
        SET_USER_INFO: 'user/SET_USER_INFO',
        SET_USER_TOKEN: 'user/SET_USER_TOKEN',
      }),
      ...mapActions({
        getUserInfo:'user/getUserInfo'
      }),
      getCode(){
        uni.login({  
          provider: 'weixin',  
          success: res => {  
            console.log(res);
            this.loginCode = res.code
          },  
          fail: err => {  
            console.log(err);
          }  
		  });
      },
      login() {
		if(!this.checked) return this.$Toast('请同意隐私协议', 1500)
        if(this.loginCode){
          uni.getUserProfile({
            desc: '用于完善个人资料',
            success: res=>{
              let code = this.loginCode
              BaseCenterApi.miniappAuth({ rawData: res.rawData, code }).then(res=>{
                console.log(res);
				this.$Toast(res.msg)
                if(res.code == 1){
					this.SET_USER_TOKEN(res.data.token)
			      	this.getUserInfo()
                  	// uni.navigateBack()
					uni.switchTab({
						url:'/pages/tabbar/index/index'
					})
                }
              })
            },
            fail: err=>{
              console.log(err);
            }
          });
        }else{
          this.getCode()
        }
      },
      toArgu(type) {
        uni.navigateTo({
          url: '/pages/login/registerArgu/registerArgu?type='+type
        })
      }
    },
  }
</script>

<style lang="scss" scoped>

.logo {
	display: flex;
	justify-content: center;
	margin-top: 100rpx;
}
</style>
